package laan.xiaonei
{
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class XiaoNeiAPI extends EventDispatcher
    {
        private var apiRequester:APIRequester;
        private var _uid:String;
        private var _session:String;
        private var _isReady:Boolean = false;
        private var _messagesCount:uint = 0;
        private var sessionTimer:Timer;
        private var _requestsCount:uint = 0;

        public function XiaoNeiAPI(apiKey:String, secretKey:String)
        {
            XiaoNeiConfig.API_KEY = apiKey;
            XiaoNeiConfig.SECRET_KEY = secretKey;
            apiRequester = new APIRequester();
            addEventListener(XiaoNeiAPIEvent.API_READY, apiReadyHandler);
            return;
        }// end function

        public function getUserInvitation(handler:Function, uids:Array) : void
        {
            if (uids)
            {
            }
            if (!uids.length)
            {
                throw new Error("The lenght of uids array must be more than 0!!");
            }
            var _loc_3:* = new Object();
            _loc_3.method = XiaoNeiMethods.GET_USER_INVITATIONS;
            _loc_3.session_key = _session;
            _loc_3.uids = uids.join(",");
            apiRequester.callAPI(_loc_3, handler);
            return;
        }// end function

        public function registerOrder(handler:Function, orderId:String, amount:uint, desc:String, test:Boolean = true) : void
        {
            var _loc_6:* = new Object();
            if (test)
            {
                _loc_6.method = XiaoNeiMethods.REGISTER_ORDER_TEST;
            }
            else
            {
                _loc_6.method = XiaoNeiMethods.REGISTER_ORDER;
            }
            _loc_6.session_key = _session;
            _loc_6.order_id = orderId;
            _loc_6.amount = amount;
            _loc_6.desc = desc;
            apiRequester.callAPI(_loc_6, handler);
            return;
        }// end function

        public function checkOrderCompleted(handler:Function, orderId:String, test:Boolean = true) : void
        {
            var _loc_4:* = new Object();
            if (test)
            {
                _loc_4.method = XiaoNeiMethods.CHECK_ORDER_COMPLETED_TEST;
            }
            else
            {
                _loc_4.method = XiaoNeiMethods.CHECK_ORDER_COMPLETED;
            }
            _loc_4.session_key = _session;
            _loc_4.order_id = orderId;
            apiRequester.callAPI(_loc_4, handler);
            return;
        }// end function

        public function sendFeed(handler:Function, templateId:uint, titleData:String, bodyData:String) : void
        {
            var _loc_5:* = new Object();
            _loc_5.method = XiaoNeiMethods.SEND_FEED;
            _loc_5.session_key = _session;
            _loc_5.template_id = String(templateId);
            _loc_5.title_data = titleData;
            _loc_5.body_data = bodyData;
            apiRequester.callAPI(_loc_5, handler);
            return;
        }// end function

        private function getSessionHandler(value:Object) : void
        {
            if (_isReady)
            {
                return;
            }
            if (value)
            {
            }
            if (value.uid)
            {
            }
            if (value.session_key)
            {
                initAPI(value.uid, value.session_key);
            }
            return;
        }// end function

        public function get secretKey() : String
        {
            return XiaoNeiConfig.SECRET_KEY;
        }// end function

        public function get uid() : String
        {
            return _uid;
        }// end function

        public function sendNotification(handler:Function, uids:Array, msg:String) : void
        {
            var _loc_4:* = new Object();
            _loc_4.method = XiaoNeiMethods.SEND_NOTIFICATION;
            _loc_4.session_key = _session;
            if (uids)
            {
                _loc_4.to_ids = uids.join(",");
            }
            else
            {
                _loc_4.to_ids = "";
            }
            _loc_4.notification = msg;
            apiRequester.callAPI(_loc_4, handler);
            return;
        }// end function

        private function getAllocationHandler(obj:Object) : void
        {
            if (obj)
            {
                _messagesCount = obj.notifications_per_day;
                _requestsCount = obj.request_per_day;
            }
            return;
        }// end function

        public function getAppAdded(handler:Function, uid:String = null) : void
        {
            var _loc_3:* = new Object();
            _loc_3.method = XiaoNeiMethods.GET_APP_ADDED;
            if (uid)
            {
                _loc_3.uid = uid;
            }
            else
            {
                _loc_3.session_key = _session;
            }
            apiRequester.callAPI(_loc_3, handler);
            return;
        }// end function

        public function get session() : String
        {
            return _session;
        }// end function

        public function isReady() : Boolean
        {
            return _isReady;
        }// end function

        public function getAppUsers(handler:Function, all:Boolean = false) : void
        {
            var _loc_3:* = new Object();
            if (all)
            {
                _loc_3.method = XiaoNeiMethods.GET_APP_USERS;
            }
            else
            {
                _loc_3.method = XiaoNeiMethods.GET_APP_USERS_ALL;
            }
            _loc_3.session_key = _session;
            apiRequester.callAPI(_loc_3, handler);
            return;
        }// end function

        public function getFriendsDetail(handler:Function, page:uint = 0, count:uint = 500) : void
        {
            var _loc_4:* = new Object();
            _loc_4.method = XiaoNeiMethods.GET_FRIENDS_DETAIL;
            _loc_4.session_key = _session;
            _loc_4.page = page;
            _loc_4.count = count;
            apiRequester.callAPI(_loc_4, handler);
            return;
        }// end function

        private function stopDetectingSession() : void
        {
            if (sessionTimer)
            {
                sessionTimer.removeEventListener(TimerEvent.TIMER, detectSessionHandler);
                sessionTimer = null;
            }
            return;
        }// end function

        private function apiReadyHandler(event:XiaoNeiAPIEvent) : void
        {
            removeEventListener(XiaoNeiAPIEvent.API_READY, apiReadyHandler);
            getAllocation(getAllocationHandler);
            return;
        }// end function

        public function get messagesCount() : uint
        {
            return _messagesCount;
        }// end function

        public function get apiKey() : String
        {
            return XiaoNeiConfig.API_KEY;
        }// end function

        private function detectSessionHandler(event:TimerEvent) : void
        {
            if (sessionTimer.currentCount == 30)
            {
                stopDetectingSession();
                return;
            }
            getSession(getSessionHandler);
            return;
        }// end function

        public function initAPI(uid:String, session:String) : void
        {
            _uid = uid;
            _session = session;
            _isReady = true;
            stopDetectingSession();
            dispatchEvent(new XiaoNeiAPIEvent(XiaoNeiAPIEvent.API_READY));
            return;
        }// end function

        public function get requestsCount() : uint
        {
            return _requestsCount;
        }// end function

        public function getLoginUser(handler:Function) : void
        {
            var _loc_2:* = new Object();
            _loc_2.method = XiaoNeiMethods.GET_LOGIN_USER;
            _loc_2.session_key = _session;
            apiRequester.callAPI(_loc_2, handler);
            return;
        }// end function

        public function toLogin() : void
        {
            var _loc_1:* = new Object();
            _loc_1.method = XiaoNeiMethods.CREATE_TOKEN;
            apiRequester.callAPI(_loc_1, toLoginHandler);
            return;
        }// end function

        public function getSession(handler:Function) : void
        {
            var _loc_2:* = new Object();
            _loc_2.method = XiaoNeiMethods.GET_SESSION;
            if (XiaoNeiConfig.TOKEN)
            {
                _loc_2.auth_token = XiaoNeiConfig.TOKEN;
            }
            apiRequester.callAPI(_loc_2, getSessionHandler);
            return;
        }// end function

        public function get version() : String
        {
            return "1.0";
        }// end function

        public function getFriendsUID(handler:Function, page:uint = 0, count:uint = 500) : void
        {
            var _loc_4:* = new Object();
            _loc_4.method = XiaoNeiMethods.GET_FRIENDS_UID;
            _loc_4.session_key = _session;
            _loc_4.page = page;
            _loc_4.count = count;
            apiRequester.callAPI(_loc_4, handler);
            return;
        }// end function

        public function getUserDetail(handler:Function, uid:String, fields:Array = null) : void
        {
            if (!fields)
            {
                fields = ["name", "sex", "star", "headurl"];
            }
            var _loc_4:* = new Object();
            _loc_4.method = XiaoNeiMethods.GET_USER_DETAIL;
            _loc_4.session_key = _session;
            _loc_4.uid = uid;
            _loc_4.fields = fields.join(",");
            apiRequester.callAPI(_loc_4, handler);
            return;
        }// end function

        private function startToDetectSession() : void
        {
            if (!sessionTimer)
            {
                sessionTimer = new Timer(3000);
                sessionTimer.addEventListener(TimerEvent.TIMER, detectSessionHandler);
            }
            else
            {
                sessionTimer.reset();
            }
            sessionTimer.start();
            return;
        }// end function

        public function getAllocation(handler:Function) : void
        {
            var _loc_2:* = new Object();
            _loc_2.method = XiaoNeiMethods.GET_ALLOCATION;
            apiRequester.callAPI(_loc_2, handler);
            return;
        }// end function

        public function getInvitationDetails(handler:Function, ids:Array) : void
        {
            if (ids)
            {
            }
            if (!ids.length)
            {
                throw new Error("The lenght of ids array must be more than 0!!");
            }
            var _loc_3:* = new Object();
            _loc_3.method = XiaoNeiMethods.GET_INVITATION_DETAILS;
            _loc_3.invite_ids = ids.join(",");
            apiRequester.callAPI(_loc_3, handler);
            return;
        }// end function

        private function toLoginHandler(value:Object) : void
        {
            var _loc_2:String = null;
            if (value)
            {
                XiaoNeiConfig.TOKEN = value.auth_token;
                _loc_2 = XiaoNeiConfig.LOGIN_URL;
                _loc_2 = _loc_2 + ("?api_key=" + XiaoNeiConfig.API_KEY);
                _loc_2 = _loc_2 + ("&v=" + XiaoNeiConfig.VERSION);
                _loc_2 = _loc_2 + ("&auth_token=" + XiaoNeiConfig.TOKEN);
                navigateToURL(new URLRequest(_loc_2), "_blank");
                startToDetectSession();
            }
            return;
        }// end function

    }
}
